package com.its.cloud.entity;

import com.fasterxml.jackson.annotation.JsonInclude;
import lombok.Data;
import lombok.NoArgsConstructor;

import java.io.Serializable;
import java.util.List;

/**
 * @author huguangjun
 * @className ResultBean
 * @date 2020/12/3
 * @desc 13:56
 */
@Data
@NoArgsConstructor
@JsonInclude(JsonInclude.Include.NON_NULL)
public class ResultBean<T> implements Serializable {
    private static final long serialVersionUID = 1L;
    /**
     * the aop method for this class unify exception handler
     * 用aop方式可以对此类统一异常捕获
     */

    public static final int SUCCESS = 0;
    public static final int UNKNOWN_EXCEPTION = -99;

    private int code = SUCCESS;
    private String msg = "success";
    private T data;

    private List<T> rows;
    private String total;

    public ResultBean(T data) {
        super();
        this.data = data;
    }

    public ResultBean(Throwable e) {
        super();
        this.msg = "fail";
        if (null != e) {
            this.data = (T) e.toString();
        } else {
            // todo: -1标识，需要查看日志失败原因
            this.data = (T) "-1操作失败.";
        }
        this.code = UNKNOWN_EXCEPTION;
    }

    public ResultBean(long total, List<T> rows) {
        this.total = String.valueOf(total);
        this.rows = rows;
    }


}
